#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QRectF>
#include <QDebug>
#include <QPalette>

#include "drawcircle.h"

DrawCircle::DrawCircle(QWidget* parent) :
        QWidget(parent)
{
    myX = 0;
    myY = 0;
    myRadius = 0;
    myWidth = 0;
    myColor = QColor(0,0,0);
    ringFlag = true;
}

DrawCircle::~DrawCircle()
{
}

void DrawCircle::endThis()
{
    this->close();
}

void DrawCircle::setCircle(int x, int y, int radius, int lineWidth, QColor color)
{
    myX = x;
    myY = y;
    myRadius = radius;
    myWidth = lineWidth;
    myColor = color;
    qDebug() << "X:" << myX;
    qDebug() << "Y:" << myY;
    qDebug() << "Radius:" << myRadius;
    qDebug() << "Line Width:" << myWidth;
    qDebug() << "Color:" << myColor;
}

void DrawCircle::keyPressEvent(QKeyEvent *keyEvent)
{
    if (keyEvent->key()==Qt::Key_Escape)
    {
        this->close();
    }
//    emit escapeKeyPressed();
}

//void DrawCircle::escapeKeyPressed()
//{
//    //TODO
//}

void DrawCircle::paintEvent(QPaintEvent *pEvent)
{
    drawACircle();
}

void DrawCircle::drawACircle()
{
    //TODO
    this->setAutoFillBackground(true);
    QPalette palette;
    palette.setColor(QPalette::Background, QColor(0,0,0));
    this->setPalette(palette);

    QPainter painter(this);
    QBrush brush;
    QPen pen;

    pen.setWidth(myWidth);
    pen.setColor(myColor);
    brush.setColor(myColor);
    if (!ringFlag) brush.setStyle(Qt::SolidPattern);

    QRectF rect(myX-myRadius, myY-myRadius, myRadius*2, myRadius*2);
    qDebug() << "QRectF:" << rect;
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawEllipse(rect);
}

void DrawCircle::setRing(bool ring)
{
    ringFlag = ring;
}
